layui.define(['table', 'form', 'upload', 'admin'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    table.render({
        elem: '#LAY-maintain-manage'
        , url: '/admin/maintain/getlist'
        , cols: [[
            {field: 'id', width: 70, title: 'ID'}
            , {field: 'title', title: '维护标题', width: 200}
            , {field: 'content', title: '维护内容', minWidth: 200}
            , {field: 'device_text', title: '维护端', width: 120}
            , {
                field: 'start_time', title: '维护时间', width: 320, templet: function (d) {
                    return d.start_time + ' - ' + d.end_time;
                }
            }
            , {field: 'remind_time', title: '提示维护时间', width: 180}
            , {field: 'update_time', title: '操作时间', width: 180}
            , {
                field: '', title: '操作人', width: 120, templet: function (d) {
                    return d.admin ? d.admin.name : '';
                }
            }
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-maintain-opera'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });


    form.on('submit(maintain-add)', function (obj) {
        var data = obj.field;
        if (data.start_time && data.end_time && data.start_time >= data.end_time) {
            return layer.msg('维护时间选择错误', {icon: 5});
        }

        if (data.start_time && data.remind_time && data.start_time < data.remind_time) {
            return layer.msg('维护时间必须大于提示维护时间', {icon: 5});
        }
        layer.load();
        //提交
        admin.req({
            url: '/admin/maintain/add'
            , data: data
            , success: function (res) {
                layer.closeAll("loading");
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                    parent.layui.table.reload("LAY-maintain-manage");
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        parent.window.location.reload();
                    }, 1200);
                }
            },
            complete: function (res) {
                layer.closeAll("loading");
            }
        });
        return false;
    });

    form.on('submit(maintain-edit)', function (obj) {
        var data = obj.field;

        if (data.start_time && data.end_time && data.start_time >= data.end_time) {
            return layer.msg('维护时间选择错误', {icon: 5});
        }

        if (data.start_time && data.remind_time && data.start_time < data.remind_time) {
            return layer.msg('维护时间必须大于提示维护时间', {icon: 5});
        }

        layer.load();

        //提交
        admin.req({
            url: '/admin/maintain/edit'
            , data: data
            , success: function (res) {
                layer.closeAll("loading");
                if (res.code == 0) {
                    parent.layui.table.reload("LAY-maintain-manage");
                    layer.msg(res.msg, {icon: 1});
                    setTimeout(function () {
                        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    }, 1200);

                }
            },
            complete: function (res) {
                layer.closeAll("loading");
            }
        });
        return false;
    });

    //监听工具条
    table.on('tool(LAY-maintain-manage)', function (obj) {
        if (obj.event === 'setStatus') {
            let status = 1;
            var confirm_str = '确定设置为生效吗';
            switch (obj.data.status) {
                case 0:
                    status = 1;
                    break;
                default:
                    status = 0;
                    confirm_str = '确定设置为失效吗？';
                    break;
            }

            layer.confirm(confirm_str, function (index) {
                layer.load();
                admin.req({
                    url: '/admin/maintain/set_status'
                    , data: {id: obj.data.id, status: status}
                    , success(res) {
                        layer.closeAll("loading");
                        if (res.code == 0) {
                            table.reload("LAY-maintain-manage");
                            layer.msg(res.msg, {icon: 1});
                        }
                    },
                    complete: function (res) {
                        layer.closeAll("loading");
                    }
                });
            });
        } else if (obj.event === 'edit') {
            let index = layer.open({
                type: 2
                , title: '编辑维护信息'
                , content: '/admin/maintain/edit?id=' + obj.data.id
                , maxmin: true
                , area: ['650px', '500px']
            });
            layer.full(index);
        }
    });


    exports('maintain', {})
});

